package com.qf.controller;/*
 *保护你的大熊
 *2021/11/29
 *16:49
 */

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.pojo.DtsGoods;
import com.qf.service.DtsBrandService;
import com.qf.service.DtsCategoryService;
import com.qf.service.DtsGoodsService;
import com.qf.util.ResponseUtil;
import com.qf.vo.CatVo;
import com.qf.vo.GoodsAllinone;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin("*")
@RestController
@RequestMapping("/admin/goods")
public class AdminGoodsController {

    @Autowired
    DtsBrandService brandService;

    @Autowired
    DtsGoodsService goodsService;

    @Autowired
    DtsCategoryService categoryService;


    @RequestMapping("/update")
    public Object update(@RequestBody GoodsAllinone goodsAllinone){
        return goodsService.update(goodsAllinone);
    }


    @RequestMapping("/catAndBrand")
    public Object catAndBrand(){

        List<CatVo> categoryList = categoryService.findCategoryList();

        List<Map<String,Object>> brandList = brandService.findBrandList();

        Map<String,Object> data = new HashMap<>();
        data.put("categoryList",categoryList);
        data.put("brandList",brandList);

        return ResponseUtil.ok(data);

    }


    @RequestMapping("/detail")
    public Object detail(Integer id){
        Map<String,Object> data = goodsService.findById(id);
        return ResponseUtil.ok(data);
    }


    @RequestMapping("list")
    public Object list(
            @RequestParam(name = "page",required = false,defaultValue = "1") Integer page,
            @RequestParam(name = "limit",required = false,defaultValue = "20") Integer limit,
            String goodSn,
            String name,
            @RequestParam(name = "sort",required = false,defaultValue = "add_time") String sort,
            @RequestParam(name = "order",required = false,defaultValue = "desc") String order){

        //是否是品牌管理员
        Subject subject = SecurityUtils.getSubject();

        List<Integer> brandIds = null;
        if(subject.hasRole("品牌制造商") && !subject.hasRole("超级管理员")){
            //如果是品牌制造商，查询所有的品牌id
            brandIds = brandService.findBreandIds();
        }


        //分页查询
        IPage<DtsGoods> iPage = goodsService.findPage(page,limit,sort,order,name,goodSn,brandIds);

        Map<String,Object> data = new HashMap<>();
        data.put("total", iPage.getTotal());
        data.put("items", iPage.getRecords());

        return ResponseUtil.ok(data);

    }

}


